package com.wunu.zn.api.appliance.model;

import java.util.List;
import java.util.Map;

import common.plugin.mybatis.Db;
import common.plugin.mybatis.Model;
import common.plugin.mybatis.Table;
@Table("t_ppmhtv_machine_bind")
public class Machine extends Model<Machine>{
	private String deviceId;
	private String model;
	private String createTime;
	public String getDeviceId() {
		return deviceId;
	}
	public void setDeviceId(String deviceId) {
		this.deviceId = deviceId;
	}
	public String getModel() {
		return model;
	}
	public void setModel(String model) {
		this.model = model;
	}
	public String getCreateTime() {
		return createTime;
	}
	public void setCreateTime(String createTime) {
		this.createTime = createTime;
	}
	@Override
	public String toString() {
		return "Machine [deviceId=" + deviceId + ", model=" + model + ", createTime=" + createTime + "]";
	}
	
	/**
	 * 查询用户设备
	 * @param cId
	 * @return
	 */
	public static Machine getMachine(String machineId) {
		String sql = "SELECT * FROM t_ppmhtv_machine_bind m  WHERE deviceId =  #{deviceId}   LIMIT 1 ";
		return Db.findFirst(Machine.class, sql, "deviceId", machineId);
		 
	}
	/**
	 * 查询用户设备列表
	 * @param openId
	 * @return
	 */
	public static List<Map<String, Object>> getMachineList(String openId) {
		
		String sql="SELECT  b.deviceId ,r.`data`,CASE WHEN b.`name` IS NULL THEN 0 ELSE b.deviceId END AS `name`,"
				+ "datediff(o.endTime,NOW()) AS days,r.model,r.state FROM t_ppmhtv_order o, t_ppmhtv_machine_bind b ,"
				+ "t_ppmhtv_machine_record r,(SELECT MAX(id) AS id  FROM t_ppmhtv_machine_record r  GROUP  BY r.deviceId )m "
				+ "WHERE b.deviceId =r.deviceId  AND r.id = m.id AND o.openId = b.openId AND o.openId = #{openId} "
				+ "ORDER BY r.createTime DESC";
		 List<Map<String, Object>> list = Db.find(sql, "openId", openId);
		  if(list!=null && list.size()>0){
			
			  return list;
			}
		
		 return null;
	}
	
    /**
     * 查询用户绑定设备
     * @param machineId
     * @param openId
     * @return
     */
	public static Machine getReBindMachine(String machineId,String openId) {
		String sql = "SELECT * FROM t_ppmhtv_machine_bind m  WHERE machineId =  #{machineId} and openId =  #{openId} and islet=true LIMIT 1 ";
		return Db.findFirst(Machine.class, sql, "machineId,openId", machineId,openId);
		 
	}
	   /**
     * 查询设备
     * @param machineId
     * @param openId
     * @return
     */
	public static Machine getBindMachine(String machineId) {
		String sql = "SELECT * FROM t_ppmhtv_machine_bind m  WHERE machineId =  #{machineId} LIMIT 1 ";
		return Db.findFirst(Machine.class, sql, "machineId", machineId);
		 
	}
}
